package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.OrderDao;
import com.itheima.domain.Order;
import com.itheima.domain.Traveller;
import com.itheima.service.OrderService;
import com.itheima.utils.MybatisUtils;

import com.itheima.domain.Member;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class OrderServiceImpl implements OrderService {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public PageInfo<Order> findAll(int page, int size) {
            OrderDao mapper = (OrderDao) MybatisUtils.getMapper(OrderDao.class);
            PageHelper.startPage(page, size);
            List<Map> mapList = mapper.findAll();
            for (Map map : mapList) {
                String orderTime = sdf.format(map.get("orderTime"));
                map.put("orderTime", orderTime);
            }
            return new PageInfo(mapList);
    }

    public Map findById(String id, String pId) {
        OrderDao mapper = (OrderDao) MybatisUtils.getMapper(OrderDao.class);
        Map order = mapper.findById(id, pId);
        String orderTime = sdf.format(order.get("orderTime"));
        String DepartureTime = sdf.format(order.get("DepartureTime"));
        order.put("orderTime", orderTime);
        order.put("DepartureTime", DepartureTime);
        return order;
    }

    @Override
    public void deleteByCheck(String[] ids) {
        OrderDao mapper = (OrderDao) MybatisUtils.getMapper(OrderDao.class);
        mapper.deleteByCheck(ids);

    }

    @Override
    public void deleteByCheckInOP(String[] ids) {
        OrderDao mapper = (OrderDao) MybatisUtils.getMapper(OrderDao.class);
        mapper.deleteByCheckInOP(ids);
    }

    @Override
    public List<Traveller> findTraveller(String id) {
        OrderDao mapper = (OrderDao) MybatisUtils.getMapper(OrderDao.class);
        return mapper.findTraveller(id);
    }

    @Override
    public Member findMember(String memberId) {
        OrderDao mapper = (OrderDao) MybatisUtils.getMapper(OrderDao.class);
        return mapper.findMember(memberId);
    }

    @Override
    public void updateStatus(String id, String orderStatus) {
        if ("1".equals(orderStatus)) {
            orderStatus = "0";
        }else if ("0".equals(orderStatus)) {
            orderStatus = "1";
        }
        OrderDao mapper = (OrderDao) MybatisUtils.getMapper(OrderDao.class);
        mapper.updateStatus(id, orderStatus);
    }


}
